#include "LoopStatusControlPage.h"


void LoopStatusControlPage::paint() {

    if (mloopGroups.empty()) {

        strStream << "searching loop" << std::right << std::setw(2) << pageNum;
        std::string first = strStream.str();

        screen.show(0, 0, first);
    } else {
        LoopGroupInfo& loopGroup = mloopGroups.at(mmultiPageNo);
        strStream << "Extend" << loopGroup.channel << std::setw(9) << std::right << pageNum;
        std::string first = strStream.str();
        resetStream();

        strStream << std::left << std::setw(9) << loopGroup.groupName << loopGroup.firstIndexInSlave << "-" << (loopGroup.firstIndexInSlave + loopGroup.portCount - 1);
        std::string second = strStream.str();

        std::string third;
        mloopManager.getLoopStatus(third);

        std::string status;
        clipStatusView(std::string_view(third), loopGroup, Screen::SCREEN_WIDTH, status);
        screen.show(&first, &second, &status);
    }
    resetStream();

}



void LoopStatusControlPage::beforeDisplay() {
    mpControllers = mloopManager.mpControllers;
    LoopController* pLoopController;
    ExtendDevicePtr extendDevicePtr;
    mloopGroups.clear();
    for (ExtendDeviceWeakPtr& loopDeviceWeakPtr : *mpControllers) {
        extendDevicePtr = loopDeviceWeakPtr.lock();
        if (!extendDevicePtr) {
            continue;
        }
        pLoopController = static_cast<LoopController*> (&*extendDevicePtr);
        pLoopController->initLoopGroupInfo(mloopGroups);
    }

    mpageSize = mloopGroups.size();

    super::beforeDisplay();
}



void LoopStatusControlPage::afterDisplay() {
    super::afterDisplay();
}



int LoopStatusControlPage::onChannelKeyPressed(const KeyCode& keyCode) {
    int currentPrefixIndex = 0;
    int result = 1;
    if (visible) {
        LoopGroupInfo& loopGroup = mloopGroups.at(mmultiPageNo);
        if (keyCode < 0 || keyCode >= loopGroup.portCount) {
            LOG_ERROR("manual option invalid key:%d,loop group size:%d", loopGroup.portCount);
            return result;
        }
        currentPrefixIndex = loopGroup.firstIndexInLoopManager;

        mloopManager.changeManualLoopStatus(currentPrefixIndex + keyCode);
        result = 2;
    } else {
        mloopManager.changeManualLoopStatus(keyCode);
    }

    return result;
}



bool LoopStatusControlPage::onLoopChanged(const size_t index, const LoopStatus status) {

    LoopSize_t tempIndex = index;
      if (visible) {
        const LoopGroupInfo& loopGroup = mloopGroups.at(mmultiPageNo);
        tempIndex = index - loopGroup.firstIndexInLoopManager;
    }
    if (0 <= tempIndex) {
        LedColor color = getColorByCommand(status);
        screen.showLed(tempIndex, color);
    }
    return true;
}



bool LoopStatusControlPage::onLoopChanged(LoopControlInfoPtr command) {

    return displayLed(command->operator std::string_view());
}


bool LoopStatusControlPage::displayLed(const std::string_view& loopStatusArray) {
    Screen::AllLed allLed;
    const LoopGroupInfo& loopGroup = mloopGroups.at(mmultiPageNo);
    std::string clippedStatus;
    clipStatusView(loopStatusArray, loopGroup, Screen::MAX_LED_COUNT, clippedStatus);
    const LoopStatus* pLoopStatusArray = reinterpret_cast <const LoopStatus*>(clippedStatus.data());
    for(LoopSize_t i = 0; i < clippedStatus.size(); ++i) {
        allLed[i] = getColorByCommand(pLoopStatusArray[i]);
    }
//    std::string data((std::string_view)*command);
//    LOG_DEDUG("LoopStatusControlPage::onLoopChanged:%s", data.data());

    screen.showAllLed(allLed);

    return true;
}



